//
//  weatherView.m
//  BiuWeather
//
//  Created by 楼楚 on 16/2/27.
//  Copyright © 2016年 Chu Lou. All rights reserved.
//

#import "weatherView.h"
#import "CityTitleView.h"
#import "UVView.h"
#import "tempView.h"
#import "SunView.h"
#import "WeatherIconView.h"
#import "WindSpeedView.h"
#import "MaxTempView.h"
#import "LeftToRightView.h"
#import "UpToDownView.h"



@interface weatherView ()<UITableViewDelegate>

@property (nonatomic, strong) UITableView          *tableView;
@property (nonatomic, strong) CityTitleView         *cityTitleView;
@property (nonatomic, strong) UVView                 *uVView;
@property (nonatomic, strong) tempView              *temperatureView;
@property (nonatomic, strong) SunView                *sunInfoView;
@property (nonatomic, strong) WeatherIconView  *weatherIconView;
@property (nonatomic, strong) WindSpeedView    *windSpeedView;
@property (nonatomic, strong) MaxTempView       *maxTempView;
@property (nonatomic, strong) LeftToRightView    *grayLine1;
@property (nonatomic, strong) LeftToRightView    *grayLine2;
@property (nonatomic, strong) LeftToRightView    *grayLine3;
@property (nonatomic, strong) LeftToRightView    *grayLine4;
@property (nonatomic, strong) UpToDownView     *vLine;
@end
@implementation weatherView

- (void)buildView
{
  
    // 加载用tableView
    self.tableView                 = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate        = self;
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.tableView];
    
    self.cityTitleView = [[CityTitleView alloc] initWithFrame:CGRectMake(0, 0, Width, Height - Width - Width / 2.f)];
    [self.cityTitleView buildView];
    [self.tableView addSubview:self.cityTitleView];
    
    if (iPhone4_4s) {
        
        
        self.uVView = [[UVView alloc] initWithFrame:CGRectMake(0, Height - Width / 2.f, Width / 2.f, Width / 2.f)];
//        self.uVView.backgroundColor = [UIColor redColor];
//
        [self.uVView buildView];
        [self.tableView addSubview:self.uVView];
        
        self.temperatureView = [[tempView alloc]initWithFrame:CGRectMake(Width / 2.f, Height - Width, Width / 2.f, Width / 2.f)];
//        self.temperatureView.backgroundColor = [UIColor blueColor];
        [self.temperatureView buildView];
        
        [self.tableView addSubview:self.temperatureView];
        
        
        self.sunInfoView = [[SunView alloc] initWithFrame:CGRectMake(Width / 2.f, Height - Width / 2.f, Width / 2.f, Width / 2.f)];
//        self.sunInfoView.backgroundColor = [UIColor greenColor];
        [self.sunInfoView buildView];
        
        [self.tableView addSubview:self.sunInfoView];
        
        self.weatherIconView = [[WeatherIconView alloc] initWithFrame:CGRectMake(0, Height - Width, Width / 2.f, Width / 2.f)];
//        self.weatherIconView.backgroundColor = [UIColor grayColor];

        [self.weatherIconView buildView];
        [self.tableView addSubview:self.weatherIconView];

        
        
    }else{
        // 显示控件
        self.uVView = [[UVView alloc] initWithFrame:CGRectMake(0, Height - Width, Width / 2.f, Width / 2.f)];
//        self.uVView.backgroundColor = [UIColor redColor];
        [self.uVView buildView];
        [self.tableView addSubview:self.uVView];
        
        self.temperatureView = [[tempView alloc]initWithFrame:CGRectMake(Width / 2.f, Height - Width - Width / 2.f, Width / 2.f, Width / 2.f)];
//        self.temperatureView.backgroundColor = [UIColor blueColor];
        [self.temperatureView buildView];
        
        [self.tableView addSubview:self.temperatureView];
        
        self.sunInfoView = [[SunView alloc] initWithFrame:CGRectMake(Width / 2.f, Height - Width , Width / 2.f, Width / 2.f)];
//        self.sunInfoView.backgroundColor = [UIColor greenColor];
        [self.sunInfoView buildView];
        [self.tableView addSubview:self.sunInfoView];
        
        self.weatherIconView = [[WeatherIconView alloc] initWithFrame:CGRectMake(0, Height - Width - Width / 2.f, Width / 2.f, Width / 2.f)];
//        self.weatherIconView.backgroundColor = [UIColor grayColor];
        [self.weatherIconView buildView];
        [self.tableView addSubview:self.weatherIconView];
        
        self.maxTempView = [[MaxTempView alloc] initWithFrame:CGRectMake(0, Height - Width / 2.f, Width / 2.f, Width / 2.f)];
        [self.maxTempView buildView];
//        self.maxTempView.backgroundColor = [UIColor yellowColor];
        [self.tableView addSubview:self.maxTempView];
        
        
        self.windSpeedView = [[WindSpeedView alloc] initWithFrame:CGRectMake(Width / 2.f, Height - Width / 2.f, Width / 2.f, Width / 2.f)];
//        self.windSpeedView.backgroundColor = [UIColor orangeColor];
        [self.windSpeedView buildView];
        [self.tableView addSubview:self.windSpeedView];

    }
    // 创建出线条
    {
        self.grayLine1                 = [[LeftToRightView alloc] initWithFrame:CGRectMake(0, Height - Width / 2.f, Width, 0.5)];
        self.grayLine1.backgroundColor = [UIColor blackColor];
        self.grayLine1.alpha           = 0.1;
        [self.tableView addSubview:self.grayLine1];
        
        self.grayLine2                 = [[LeftToRightView alloc] initWithFrame:CGRectMake(0, Height - 1, Width, 0.5)];
        self.grayLine2.backgroundColor = [UIColor blackColor];
        self.grayLine2.alpha           = 0.1;
        [self.tableView addSubview:self.grayLine2];
        
        self.grayLine3                 = [[LeftToRightView alloc] initWithFrame:CGRectMake(0, Height - Width, Width, 0.5)];
        self.grayLine3.backgroundColor = [UIColor blackColor];
        self.grayLine3.alpha           = 0.1;
        [self.tableView addSubview:self.grayLine3];
        
        self.grayLine4                 = [[LeftToRightView alloc] initWithFrame:CGRectMake(0, Height - Width - Width / 2.f, Width, 0.5)];
        self.grayLine4.backgroundColor = [UIColor blackColor];
        self.grayLine4.alpha           = 0.1;
        [self.tableView addSubview:self.grayLine4];
        
        self.vLine                     = [[UpToDownView alloc] initWithFrame:CGRectMake(Width / 2.f - 1, Height - Width - Width / 2.f, 0.5, Width + Width / 2.f)];
        self.vLine.backgroundColor     = [UIColor blackColor];
        self.vLine.alpha               = 0.1;
        [self.tableView addSubview:self.vLine];
    }
    

    
    
}


-(void)show{

    self.uVView.percent = self.today.humidity.intValue/100.f;
    
    self.windSpeedView.windSpeed = self.today.speed.floatValue*5/18;
    
    self.windSpeedView.circleByOneSecond = self.windSpeedView.windSpeed/10.f;
    
    self.temperatureView.temperature = self.today.temp.intValue;
    self.maxTempView.maxTemp = self.weekForcast.maxTmp.intValue;
    self.maxTempView.minTemp = self.weekForcast.minTmp.intValue;
    
    self.cityTitleView.weatherNumber = self.today.weatherCode;
    
    self.sunInfoView.sunRiseString = self.weekForcast.sunRiseTime;
    self.sunInfoView.sunSetString = self.weekForcast.sunSetTime;
    
    self.weatherIconView.weatherNumber = self.today.weatherCode;
    
    self.cityTitleView.cityName = self.cityName;
    
    self.cityTitleView.weatherDescription = self.today.weatherTxt;
    
    self.windSpeedView.weatherDis = self.today.direction;

    
    
    [self.windSpeedView show];
    [self.uVView show];
    [self.maxTempView show];
    [self.sunInfoView show];
    [self.temperatureView show];
    [self.weatherIconView show];
    [self.cityTitleView show];
    [self.grayLine1 show];
    [self.grayLine2 show];
    [self.grayLine3 show];
    [self.grayLine4 show];
    [self.vLine show];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)hide
{
    
    [self.windSpeedView hide];
    [self.uVView hide];
    [self.maxTempView hide];
    [self.sunInfoView hide];
    [self.temperatureView hide];
    [self.weatherIconView hide];
    [self.cityTitleView hide];
    
    [self.grayLine1 hide];
    [self.grayLine2 hide];
    [self.grayLine3 hide];
    [self.grayLine4 hide];
    [self.vLine hide];

}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (scrollView.contentOffset.y >= 60.f){
        if (_delegate&&[_delegate respondsToSelector:@selector(pullUpEvent)]) {
        [_delegate pullUpEvent];
//            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }

    }
        if (scrollView.contentOffset.y <= -60.f) {
            if (_delegate && [_delegate respondsToSelector:@selector(pullDownToRefreshData)]) {
                
                [_delegate pullDownToRefreshData];
            }

    }
}
@end
